package com.woniuxy.meal.schedules;

import com.woniuxy.meal.service.FoodMonthSaleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

@Component
public class ScheduleTask implements SchedulingConfigurer {
    @Value("${schedules.storesaletask.cron}")
    private String changeSaleTask;

    @Value("${schedules.foodsaletask.cron}")
    private String testTask;

    @Autowired
    private FoodMonthSaleService foodMonthSaleService;

    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
//        System.out.println(changeSaleTask);
        taskRegistrar.addTriggerTask(()-> {
            try {
                foodMonthSaleService.updateStoreSale();
                foodMonthSaleService.updateStoreSaleES();
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }, triggerContext -> {
            CronTrigger cronTrigger = new CronTrigger(changeSaleTask);
            return cronTrigger.nextExecutionTime(triggerContext);
        });
        System.out.println(testTask);
        taskRegistrar.addTriggerTask(() -> {
            try {
                foodMonthSaleService.updateFoodSale();
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }, triggerContext -> {
            CronTrigger cronTrigger = new CronTrigger(testTask);
            return cronTrigger.nextExecutionTime(triggerContext);
        });

    }

    public void setChangeSaleTask(String changeSaleTask) {
        this.changeSaleTask = changeSaleTask;
    }

    public void setTestTask(String testTask) {
        this.testTask = testTask;
    }
}
